<?php


namespace app\zwt\controller;
use app\zwt\model\BannerModel;
use app\zwt\validate\BannerValidate;
use cmf\controller\AdminBaseController;
use think\facade\Db;

class BannerController extends AdminBaseController
{
    //显示页面
    public function index(){
        $list = BannerModel::order('id','DESC')->paginate(10);
        return $this->fetch('index',[
            'list'=>$list
        ]);
    }
//    //显示banner图-轮播图
//    public function banner(){
//        return "<a href='add'>upload</a>";
//    }

    //上传图片
    public function add(){
        $list = BannerModel::select();
//        $this->assign('list');
        return $this->fetch('add',[
            'list'=>$list,
        ]);
    }
    public function addPost()
    {
        $data = input('post.');

        //验证数据
        $validate = new BannerValidate();
        $res = $validate->scene('add')->check($data);
        if (!$res) {
            $this->error($validate->getError());
        }
        $image = $data['image'];
        $url = cmf_get_image_url($image);
        $url1 = input('url');
        //入库
        if (BannerModel::create([

            'image' => $url,
            'url' => $url1,
        ])) {
            $this->success('添加成功', 'Banner/index');
        } else {
            $this->error('添加失败');
        }
    }
    //编辑
    public function edit(){
        $id = input('id');
        $list = BannerModel::find($id);
        return $this->fetch('edit',[
            'list' => $list
        ]);
    }
    public function editPost(){
        $id = input('id');
        $image = input('image');
        $url = cmf_get_image_url($image);
        $url1 = input('url');
        //查询
        $list = BannerModel::find($id);
        if(!$list){
            $this->error('未查询的记录');
        }
        //入库
        if($list->update(['image' => $url,],['id'=>$id],['url'=>$url1])){
            $this->success('保存成功','Banner/index');
        }else{
            $this->error('保存失败');
        }
    }
    //单条删除
    public function delete(){
        $id = input('id/d','');
        $isExist = BannerModel::find($id);
        //判断该id是否存在
        if(!$isExist){
            $this->error('该数据不存在');
        }
        $del = BannerModel::destroy($id);
        if($del){
            $this->success('删除成功','Banner/index');
        }else{
            $this->error('删除失败');
        }
    }
    //排序
//    public function order()
//    {
//        $model = new BannerModel();
//        parent::listOrders($model);
//        $this->success('排序成功');
//    }
}
